﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaLibrary;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Urbania.World;

namespace Urbania.GUI.Elements
{
    public class RightClickWorldObject : SelectableWorld
    {
        protected string text;
        protected string script;
        protected LuaVM lua;
        protected bool usingFile;
        protected Color textColor;
        protected bool extendable;
        protected bool useName;
        protected string cmd;
        protected bool usingStats;

        public string Text { get { return this.text; } }

        public RightClickWorldObject(string text)
        {
            this.text = text;
            this.script = null;
            this.usingFile = true;
            this.lua = Program.lua;
        }

        public RightClickWorldObject(string text, bool usingFile, bool extendable)
        {
            this.text = text;
            this.usingFile = usingFile;
            this.extendable = extendable;
            if (extendable)
                this.text += "  >";
            this.lua = Program.lua;
        }

        /// <summary>
        /// will not use the name of the object for the Lua script.
        /// </summary>
        /// <param name="text">The name of the object</param>
        /// <param name="extendable">Does the menu cascade</param>
        /// <param name="cmd">the cmd to input to the lua vm</param>
        public RightClickWorldObject(string text, bool extendable, string cmd)
        {
            this.text = text;
            this.usingFile = false;
            this.useName = false;
            this.cmd = cmd;
            this.extendable = extendable;
            if (extendable)
                this.text += "  >";
            this.lua = Program.lua;
        }

        public RightClickWorldObject(string text, bool extendable, string cmd, bool usingStats)
        {
            this.text = text;
            this.usingFile = false;
            this.useName = false;
            this.cmd = cmd;
            this.extendable = extendable;
            if (extendable)
                this.text += "  >";
            this.lua = Program.lua;
            this.usingStats = usingStats;
        }

        public RightClickWorldObject(string text, string scriptFile)
        {
            this.text = text;
            this.script = scriptFile;
            this.usingFile = true;
            this.lua = Program.lua;
        }

        public void Update(GameTime gameTime, World.Tile o, RightClickMenu r)
        {
            base.Update();

            if (this.mouseIsIn)
                this.textColor = Color.Yellow;
            else
                this.textColor = Color.White;

            if (this.mouseClickingOn)
            {
                if (extendable)
                {
                    GUI.Elements.RightClickMenu rcm = new GUI.Elements.RightClickMenu(
                        new Vector2(this.bounds.Right, this.bounds.Bottom), Color.Blue, MenuType.TileSelection);
                    Components.WorldComponent.container.AddMenu(rcm);
                    rcm.Generate(o);
                }
                else
                {
                    OnClick(o);
                    r.Remove();
                }
            }
        }

        public void Draw(Vector2 position, SpriteFont font, SpriteBatch spriteBatch)
        {
            this.bounds = new Rectangle((int)position.X, (int)position.Y, (int)font.MeasureString(this.text).X,
                (int)font.MeasureString(this.text).Y);
            spriteBatch.DrawString(font, text, position, textColor);
        }

        public void OnClick(World.Tile o)
        {
            if (usingFile)
            {
                if (this.script != null)
                {
                    lua.LuaVirtual.DoFile(Utilities.Loader.GetScriptFile(script));
                }
            }
            else if (usingStats)
            {
                Components.WorldComponent.container.GetBaseFrame().Elements.Add(new GUI.StatsBox(
                    World.TileTypeHelperClass.TileEnumExchange[
                    World.TileTypeHelperClass.TileTypeNames.ToList<string>().
                    IndexOf(this.text)]
                    ,o, Utilities.MouseWrap.PositionWorld));
            }
            else
            {
                string preComp;

                if (useName)
                    preComp = this.text;
                else
                    preComp = this.cmd;

                if (extendable)
                    preComp = preComp.Remove(preComp.Length - 3);

                preComp += "(" + o.PosIndex.X + "," + o.PosIndex.Y;
                preComp += ",'" + this.text + "'";
                preComp += ")";

                lua.LuaVirtual.DoString(preComp);
            }
        }

        public void OnClick(World.Tile o, params string[] args)
        {
            if (usingFile)
            {
                if (this.script != null)
                {
                    lua.LuaVirtual.DoFile(Utilities.Loader.GetScriptFile(script));
                }
            }
            else
            {
                string preComp = this.text + "(" + o.PosIndex.X + "," + o.PosIndex.Y;

                foreach (string s in args)
                {
                    preComp += "," + s;
                }

                preComp += ")";
                lua.LuaVirtual.DoString(preComp);
            }
        }
    }
}
